package com.yunji.news.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunji.news.common.Result;
import com.yunji.news.entity.Article;
import com.yunji.news.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 管理后台-文章管理控制器
 *
 * @author yunji
 * @since 2024-11-23
 */
@RestController
@RequestMapping("/admin/articles")
@CrossOrigin
public class AdminArticleController {
    
    @Autowired
    private ArticleService articleService;
    
    /**
     * 分页查询文章列表（包含草稿）
     */
    @GetMapping
    public Result<IPage<Article>> getArticles(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "20") Integer size,
            @RequestParam(required = false) Long categoryId,
            @RequestParam(required = false) Long schoolId,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) String keyword) {
        
        IPage<Article> result = articleService.getArticlePage(page, size, categoryId, schoolId, null, null, keyword);
        return Result.success(result);
    }
    
    /**
     * 获取文章详情
     */
    @GetMapping("/{id}")
    public Result<Article> getArticle(@PathVariable Long id) {
        Article article = articleService.getById(id);
        if (article == null) {
            return Result.error("文章不存在");
        }
        return Result.success(article);
    }
    
    /**
     * 创建文章
     */
    @PostMapping
    public Result<String> createArticle(@Valid @RequestBody Article article) {
        boolean success = articleService.createArticle(article);
        if (success) {
            return Result.success("创建成功");
        }
        return Result.error("创建失败");
    }
    
    /**
     * 更新文章
     */
    @PutMapping("/{id}")
    public Result<String> updateArticle(@PathVariable Long id, @Valid @RequestBody Article article) {
        article.setId(id);
        boolean success = articleService.updateArticle(article);
        if (success) {
            return Result.success("更新成功");
        }
        return Result.error("更新失败");
    }
    
    /**
     * 删除文章
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteArticle(@PathVariable Long id) {
        boolean success = articleService.deleteArticle(id);
        if (success) {
            return Result.success("删除成功");
        }
        return Result.error("删除失败");
    }
    
    /**
     * 发布文章
     */
    @PutMapping("/{id}/publish")
    public Result<String> publishArticle(@PathVariable Long id) {
        boolean success = articleService.publishArticle(id);
        if (success) {
            return Result.success("发布成功");
        }
        return Result.error("发布失败");
    }
    
    /**
     * 下线文章
     */
    @PutMapping("/{id}/unpublish")
    public Result<String> unpublishArticle(@PathVariable Long id) {
        boolean success = articleService.unpublishArticle(id);
        if (success) {
            return Result.success("下线成功");
        }
        return Result.error("下线失败");
    }
}